function [Yhat] = KernelRegression(y,X,x_bar,varargin)
% Kernel regression of y on X at x_bar points.
% X is single column (one variable)

Nhat = length(x_bar);
N    = size(X,1);

BigX = X*ones(1,Nhat) - ones(N,1)*x_bar'; 

nVarargs = length(varargin);

if nVarargs == 0; c = 1.06; else c = varargin{1}; end

h = c*nanstd(X)*N^(-1/5);% bandwidth: Rule of thumb... h = c * std(x) * T^(-1/5)

    
                                                              
opt.k = 1;

K = kernel(BigX/h, opt);
BigY = y*ones(1,Nhat);

Yhat = sum(BigY.*K,1)./sum(K,1);

end